<!--这是问题详情页未往下拉的时候的问题描述导航栏-->
<template>
  <div class="WholeBox" ref="DetailedHeader">
    <div class="QuestionHeader">
      <div class="QuestionBox">
        <h1 class="QuestionTitle">{{question.question_title}}</h1>
        <p class="QuestionContext">{{question.question_content}}</p>
      </div>
      <div class="QuestionHeaderFooter">
        <div class="ButtonBox">
          <el-button :type="this.attention?'info':'primary'" plain class="btn" @click="attentionQuestion">{{this.attention?"已关注":"关注问题"}}</el-button>
          <el-button :type="this.like?'info':'primary'" plain class="btn" @click="likeQuestion">{{this.like?"已点赞":"点赞"}}</el-button>
          <router-link :to="`/editAnswer?question_id=${this.question.question_id}&title=${this.question.question_title}`">
            <el-button type="primary" :icon="EditPen" plain class="btn">我要回答</el-button>
          </router-link>
          <el-button :icon="ChatLineRound" plain class="btn" @click="QComments">查看评论</el-button>
        </div>
      </div>
    </div>
    <div class="QuestionHeaderSide">
      <div class="FollowStatus">
        <div class="PublishTimeBox">
          <el-text class="PublishTimeText">发布时间</el-text>
          <el-text class="PublishTime">{{question.create_time}}</el-text>
        </div>
        <div class="LikeNumberBox">
          <el-text class="LikeNumberText">获赞数</el-text>
          <el-text class="LikeNumber">{{question.liked_count}}</el-text>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {EditPen,ChatLineRound} from "@element-plus/icons-vue";
export default {
  name: "QuestionDetailedHeader",
  props: ['question','user','attention','like'],
  setup(){
    return {
      EditPen,
      ChatLineRound
    }
  },
  data() {
    return {
      height:0//整个子控件的高度，需要传递给父控件
    }
  },
  methods: {
    attentionQuestion(){
      if(!this.attention){
        this.$axios
            .post(`${this.$target}/follow?from_id=`+this.user.id+`&to_id=`+this.question.question_id+`&type=1`)
            .then(res=>{
              if(res.data.data){
                this.notifySucceed("关注成功!")
                this.$emit('attentionQuestion')
              }else{
                this.notifyError("关注失败")
              }
            })
      }else{
        this.$axios
            .delete(`${this.$target}/follow?from_id=`+this.user.id+`&to_id=`+this.question.question_id+`&type=1`)
            .then(res=>{
              if(res.data.data){
                this.notifySucceed("取关成功!")
                this.$emit('attentionQuestion')
              }else{
                this.notifyError("取关失败")
              }
            })
      }
    },
    likeQuestion(){
      //未点赞
      if(!this.like){
        this.$axios
            .patch(`${this.$target}/question/like?question_id=`+this.question.question_id+`&user_id=`+this.user.id)
            .then(res=>{
              if(res.data.data){
                this.notifySucceed("点赞成功!")
                this.$emit('likeQuestion')
              }else{
                this.notifyError("点赞失败")
              }
            })
      }else{
        this.$axios
            .delete(`${this.$target}/question/like?question_id=`+this.question.question_id+`&user_id=`+this.user.id)
            .then(res=>{
              if(res.data.data){
                this.notifySucceed("取消点赞成功!")
                this.$emit('likeQuestion')
              }else{
                this.notifyError("取消点赞失败")
              }
            })
      }
    },
    QComments(){
      this.$emit('ShowQComments')
    }
  },
  mounted() {
    setTimeout(()=>{
      this.height = this.$refs.DetailedHeader.offsetHeight
      console.log(this.height)
      this.$emit('setHeight', this.height)
    },400)
  }
}
</script>

<style scoped lang="less">
.WholeBox {
  width: 100%;
  height: fit-content;
  margin-top: -20px;
  box-shadow: 0px 1px 0px 0px rgba(50, 50, 93, 0.25),
    0px -1px 0px 0px rgba(50, 50, 93, 0.25);
  display: flex;
  justify-content: space-around;
  .QuestionHeader {
    display: flex;
    flex-direction: column;
    width: 800px;

    .QuestionBox{
      width: 100%;
      height: fit-content;
      .QuestionTitle {
        width: 700px;
        margin: 10px auto 0;
        text-align: left;
        font-size: 44px;
        word-wrap: break-word;
      }
      .QuestionContext {
        width: 700px;
        height: fit-content;
        margin: 10px auto 0;
        text-align: left;
        font-size: 16px;
        word-wrap: break-word;
      }
    }
    .QuestionHeaderFooter {
      .ButtonBox {
        margin: 20px auto 20px;
        width: 860px;
        display: flex;
        justify-content: left;
        .btn {
          margin-right: 20px;
        }
      }
    }
  }
  .QuestionHeaderSide {
    display: flex;
    flex-direction: column;
    justify-content: center;
    .FollowStatus {
      width: 115%;
      height: 60%;
      display: flex;
      justify-content: space-between;
      .PublishTimeBox {
        height: 100%;
        display: flex;
        flex-direction: column;
        justify-content: center;
        box-shadow: 1px 0px 0px 0px rgba(50, 50, 93, 0.25);
        padding-right: 25px;
        .PublishTimeText {
          font-size: 18px;
        }
        .PublishTime {
          font-size: 26px;
          font-weight: 16;
          margin-top: 10px;
        }
      }
      .LikeNumberBox {
        height: 100%;
        display: flex;
        flex-direction: column;
        justify-content: center;
        .LikeNumberText {
          font-size: 18px;
        }
        .LikeNumber {
          font-size: 26px;
          font-weight: 16;
          margin-top: 10px;
        }
      }
    }
  }
}
</style>